export default {
    bind(el, binding) {
        const duration = parseInt(binding.value) || 3;
        //保存原始 DOM 结构
        const originalHTML = el.innerHTML;
        function startCountdown() {
            let countdown = duration;
            // 创建 loading 动画的 DOM 结构
            const spinner = document.createElement('i');
            spinner.className = 'el-icon-loading'; // Element UI 的 loading 图标类名
            spinner.style.marginRight = '5px'; // 添加间距
            // 插入 loading 图标到按钮中
            el.innerHTML = ''; // 清空按钮内容
            el.appendChild(spinner); // 添加 loading 图标
            el.insertAdjacentText('beforeend', `${countdown}秒`);
            el.disabled = true;  //禁用按钮点击事件
            const intervalId = setInterval(() => {
                countdown--;
                if (countdown <= 0) {
                    clearInterval(intervalId);
                    el.innerHTML = originalHTML; // 恢复原始 DOM 结构
                    el.disabled = false;
                } else {
                    el.lastChild.nodeValue = `${countdown}秒`;
                }
            }, 1000);
        }
        el.startCountdown = startCountdown;
        el.addEventListener('click', startCountdown);
    },
    unbind(el) {
        el.removeEventListener('click', el.startCountdown);
    },
};



